/**
 * zmn文章模块-文章编辑
 * 文件命名格式：zmn.模块.表/功能.jas
 * @author duanzuocai
 * @since 2018/10/18
 */
layui.define(['form', 'layedit', 'laydate'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var layedit = layui.layedit;

    form.render(null, "zmn-area-form");

    zmnBaseArea.laySelectSpecial(form,'/area/listCityList.action','type','mid');

    $("#mid_div").hide();

    let area = json.area;
    // if(area.level == 3 && area.type == 2){
    //     $("#mid_div").show();
    // }

    // 自定义验证规则
    form.verify({
        title: function (value) {
            if (value.length < 2) {
                return "分类名称不能少于2个字符";
            };
        }, checkLong: function (value) {
            var long = parseFloat(value);
            if(long < -180 || long > 180){
                return '经度范围：-180.0000~180.0000!';
            }
        }, checkLat: function (value) {
            var lat = parseFloat(value);
            if(lat < -90 || lat > 90){
                return '纬度范围：-90.0000~90.0000!';
            }
        },
        minSort: function(value, item){ //value：表单的值、item：表单的DOM对象
            if (value < 5000){
                return "排序号不能小于5000";
            }
        }
    });

    zmnLayUtil.layFormVerify(form);

    // 提交
    form.on('submit(zmn-area-form-submit)', function (obj) {
        var openId = layer.open({type: 3});
        var data = obj.field;
        var url = "/area/addEdit.action";

        // if(data.type == 2 && data.level == 3 && zmnUtil.isEmptyNull(data.mid)){
        //     layer.alert("请选择管理城市");
        //     return false;
        // }
        $.ajax({
            async: false,
            type: "POST", url: url,
            contentType: "application/json", dataType: "json", data: JSON.stringify($.extend(json.area,data)),
            success: function (json) {
                layer.close(openId);
                layer.msg(json.message);
                if (json.status == 200 && json.id > 0) {
                    setTimeout(function () {
                        //关闭当前弹出层
                        parent.location.reload();
                    },1000)
                }

            }
        })
        return false;
    });

$("#cancleBtn").click(function () {
    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
    parent.layer.close(index); //再执行关闭
    return false;
});
    //对外暴露的接口
    exports('art_area_detail', {});
});

var zmnBaseArea = {

    laySelectSpecial: function(form, url1,  selectOneId, selectTwoId) {
        let $ = layui.$;
        let parentId;
        form.on("select(" + selectOneId + ")", function (obj) {
            if(zmnUtil.isNull(obj) || obj.value == 0) {
                return;
            }
            parentId = obj.value;
            if(parentId == 1){
                $("#mid_div").hide();
                return;
            }else {
                $("#mid_div").show();
            }
            $.ajax({
                type: "GET", url: url1,
                dataType: "json", data: { parentId: json.area.parentId },
                success: function (items) {
                    var optionHtml = '<option value="">选择</option>';
                    for (var i = 0; i < items.length; i++) {
                        optionHtml += '<option value="' + items[i][0] + '">' + items[i][1] + '</option>';
                    }
                    $("#" + selectTwoId).html(optionHtml);
                    form.render("select");
                }
            });
        });

    },
}